from sqlmodel import select
from .dbconfig import get_session
from model.user_model import Email
import traceback


# 数据库操作函数
async def get_email(username: str = None) -> Email | None:
    """根据邮箱配置"""
    try:
        print("开始获取邮箱配置...")
        session = next(get_session())
        
        result = session.exec(select(Email))
        email = result.first()
        
        if email:
            print(f"找到邮箱配置: id={email.id}, sender={email.sender}")
            return email
        else:
            print("未找到邮箱配置")
            return None
            
    except Exception as e:
        print(f"获取邮箱配置出错: {str(e)}")
        traceback.print_exc()
        return None